package com.example.testanimation.objectanimation;

import java.util.ArrayList;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
@SuppressLint("NewApi")
public class BounceAnimationActiivty extends Activity{

	MyView myView = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		myView = new MyView(this);
		setContentView(myView);

	}
	
	
	class MyView extends View {

		ArrayList<ShapeHolder> balls = new ArrayList<ShapeHolder>();
		
		public MyView(Context context) {
			super(context);
		}
		
		public void startAnimation(){
			
		}
		

		@Override
		public boolean onTouchEvent(MotionEvent event) {
			 if (event.getAction() != MotionEvent.ACTION_DOWN &&
	                    event.getAction() != MotionEvent.ACTION_MOVE) {
	                return false;
	            }
			 
			final ShapeHolder holder = new ShapeHolder();
			holder.setX(event.getX());
			holder.setY(event.getY());
			balls.add(holder);
			long duration = (long) (500 * ((getHeight() - event.getY()) / getHeight()));
			
			ObjectAnimator animationDown = ObjectAnimator.ofFloat(holder, "y",event.getY(), getHeight() - holder.getHeight());
			animationDown.setDuration(duration);
			animationDown.setInterpolator(new AccelerateDecelerateInterpolator());
			
			ObjectAnimator animationUp = ObjectAnimator.ofFloat(holder, "y", getHeight() - holder.getHeight(),event.getY());
			animationUp.setDuration(duration);
			animationUp.setInterpolator(new DecelerateInterpolator());
			animationUp.addListener(new AnimatorListener() {
				
				@Override
				public void onAnimationStart(Animator animation) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void onAnimationRepeat(Animator animation) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void onAnimationEnd(Animator animation) {
					// TODO Auto-generated method stub
					balls.remove(holder);
				}
				
				@Override
				public void onAnimationCancel(Animator animation) {
					// TODO Auto-generated method stub
					
				}
			});
			
			ObjectAnimator animationStretch1 = ObjectAnimator.ofFloat(holder, "x", holder.getX(),holder.getX() - 50);
			animationStretch1.setDuration(duration / 4);
			animationStretch1.setRepeatMode(ObjectAnimator.REVERSE);
			animationStretch1.setRepeatCount(1);
			animationStretch1.setInterpolator(new DecelerateInterpolator());
			ObjectAnimator animationStretch2 = ObjectAnimator.ofFloat(holder, "width", holder.getWidth() ,holder.getWidth() + 100);
			animationStretch2.setDuration(duration / 4);
			animationStretch2.setRepeatMode(ObjectAnimator.REVERSE);
			animationStretch2.setRepeatCount(1);
			animationStretch2.setInterpolator(new DecelerateInterpolator());
			
			ObjectAnimator squash1 = ObjectAnimator.ofFloat(holder, "y" ,getHeight() - holder.getHeight() , getHeight() - holder.getHeight() + 50);
			squash1.setRepeatCount(1);
			squash1.setRepeatMode(ValueAnimator.REVERSE);
			squash1.setDuration(duration/4);
			squash1.setInterpolator(new DecelerateInterpolator());
			ObjectAnimator squash2 = ObjectAnimator.ofFloat(holder, "height" , holder.getHeight(), holder.getHeight() - 50);
			squash2.setRepeatCount(1);
			squash2.setRepeatMode(ValueAnimator.REVERSE);
			squash2.setDuration(duration/4);
			squash2.setInterpolator(new DecelerateInterpolator());
			
			AnimatorSet set = new AnimatorSet();
			set.play(animationDown).before(animationStretch1);
			set.play(animationStretch1).with(animationStretch2);
			set.play(animationStretch1).with(squash1);
			set.play(animationStretch1).with(squash2);
			set.play(animationUp).after(animationStretch2);
			
			set.start();
			return true;
		}
		
		@Override
		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);
			for (ShapeHolder shapeHolder : balls) {
				canvas.save();
				canvas.translate(shapeHolder.getX(), shapeHolder.getY());
				shapeHolder.getShape().draw(canvas);
				canvas.restore();
			}
			invalidate();
		}
		
	}
	
}
